package com.better517na.elasticseach.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class FileUtils {

    public static String getProperties(String name) {
        Properties props = getPropertiesInCache();
        String value = props.getProperty(name);
        return value;
    }

    public static Properties getPropertiesInCache() throws SecurityException {
        Properties pro = null;
        if (pro == null) {
            pro = getProperties();
        }
        return pro;
    }

    private static Properties getProperties() {
        try {
            InputStream in = Thread.currentThread().getContextClassLoader().getResource("application.properties").openStream();
            Properties props = new Properties();
            props.load(in);
            in.close(); // 别忘了关流
            return props;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void putPropertiesToCache() {
        Properties pro = getProperties();
    }


    public static void appendFile(String fileName, String content) {
        try {
            File f = new File(fileName);
            if (!f.exists()){
                f.createNewFile();
            }
            //打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件,如果为 true，则将字节写入文件末尾处，而不是写入文件开始处
            FileWriter writer = new FileWriter(fileName, true);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(new String(content.getBytes("utf-8"), "utf-8"));
            bw.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static String readFile(String fileName) {
        StringBuilder sb = new StringBuilder();
        File file = new File(fileName);
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(file),"UTF-8");
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                sb.append((char) tempchar);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


    public static List<String> getFiles(String dir){
        List<String> files = new ArrayList<String>();
        File file = new File(dir);
        File[] fs = file.listFiles();
        for(File f:fs){
            if(!f.isDirectory()){
                files.add(f.getAbsolutePath());
            }

        }
        return files;
    }

    public static List<String> getFiles(String dir, String prefix){
        List<String> files = new ArrayList<String>();
        File file = new File(dir);
        File[] fs = file.listFiles();
        for(File f:fs){
            if(!f.isDirectory() && f.getName().startsWith(prefix)){
                files.add(f.getAbsolutePath());
            }
        }
        return files;
    }

}
